using Client.Framework;
using System.Text.RegularExpressions;
using UnityEngine;

/// <summary>
/// UI接口
/// </summary>
public interface ICreateWindow
{
    /// <summary>
    /// 创建预览窗口
    /// </summary>
    void CreateWindow(GameObject go, bool script);
}

/// <summary>
/// UI信息
/// </summary>
public class UIInfo
{
    /// <summary>
    /// 字段  
    /// </summary>
    public string Field { get; set; }
    /// <summary>
    /// 语句段
    /// </summary>
    public string Body { get; set; }
    /// <summary>
    /// 名字
    /// </summary>
    public string Name { get; private set; }
    /// <summary>
    /// 导出字段
    /// </summary>
    public bool IsSelected { get; set; }

    public UIInfo(string name)
    {
        IsSelected = true;
        Name = name;
    }

    public void InitCSInfo(System.Type type, string path)
    {
        Body = "\n\t\t"; string typeName = UITools.GetTypeFullName(type);
        Field = string.Format("public {0} {1} ", typeName, Name);
        Field = "\n\t" + Field + "{ get; private set; }";

        if (type == typeof(Transform))
        {
            Body += string.Format("{0} = Tf.Find(\"{1}\");", Name, path);
        }
        else if (type == typeof(GameObject))
        {
            Body += string.Format("{0} = Tf.Find(\"{1}\").gameObject;", Name, path);
        }
        else
        {
            Body += string.Format("{0} = Tf.Find(\"{1}\").GetComponent<{2}>();", Name, path, typeName);
        }
    }

    public void InitLuaInfo(System.Type type, string path)
    {
        Body = "\n\t"; string typeName = "CS." + type.FullName;
        Field = string.Format("\n---@field public {0} {1}", Name, typeName);

        if (type == typeof(Transform))
        {
            Body += string.Format("self.{0} = self:FindTransform('{1}')", Name, path);
        }
        else if (type == typeof(GameObject))
        {
            Body += string.Format("self.{0} = self:FindGameObject('{1}')", Name, path);
        }
        else
        {
            Body += string.Format("self.{0} = self:GetComponent('{1}', {2})", Name, path, typeName);
        }
    }
}

namespace Client.Framework
{
    public class UITools
    {
        static string pattern = "^[a-zA-Z$_]+[a-zA-Z0-9$_]*$";

        public static bool IsValidName(string objName)
        {
            return Regex.IsMatch(objName, pattern);
        }

        public static string GetTypeString(Component com)
        {
            if (com is SerializeRoot)
            {
                return "slr";
            }
            else if (com is SerializeNode)
            {
                return "sln";
            }
            else if (com is RectTransform)
            {
                return "rtf";
            }
            else if (com is Transform)
            {
                return "tf";
            }
            else { return com.GetTypeName(); }
        }

        public static string GetTypeFullName(System.Type type)
        {
            if (type.Namespace == "UnityEngine" ||
                type.Namespace == "UnityEngine.UI")
            {
                return type.Name;
            }
            return type.FullName;
        }
    }
}